import O1HD from "../../engines/gameEngine";
import level_bunchbunchbunch from "./leve_bunchbunchbunch";
import level_bunchAnimalItem from "./level_bunchAnimalItem";

const {ccclass, property} = cc._decorator;

@ccclass
export default class level_bunchAnimalGroup extends cc.Component {

    @property({type:cc.Node,tooltip:"显示节点"})
    showNode:cc.Node = null;

    @property({type:cc.Node,tooltip:"提示节点"})
    hintNode:cc.Node = null;

    @property({type:cc.Label,tooltip:"提示文本节点"})
    hintLb:cc.Label = null;

    groupTag:number = -1;

    level:level_bunchbunchbunch = null;

    maxCount:number = 0;

    animalAry:level_bunchAnimalItem[] = [];

    closeDoorFlag:boolean = false;

    @property({type:cc.Label})
    lb:cc.Label = null;
    
    bindTag(level:level_bunchbunchbunch,tag:number){
        this.level = level;
        this.groupTag = tag;
        this.lb.string = (tag + 1).toString();
    }

    setMaxCount(maxCount:number){
        this.maxCount = maxCount;
        this.showNode.height = 55 + 80 * maxCount;
        this.hintNode.height = 55 + 80 * maxCount;
        this.hintNode.active = false;
        this.hintLb.node.height = 80 * maxCount
    }

    start(){

    }

    getLastTypeAnimal(){
        return this.animalAry[this.animalAry.length-1];
    }

    /**
     * 判断是否可以加入动物
     */
    checkCanAddAnimal(animal:level_bunchAnimalItem){
        if(this.animalAry.length >= this.maxCount){
            // O1HD.getModules().uiControlModule.showMessageBox("已满");
            return false;
        }
        let last = this.getLastTypeAnimal();
        if(!last){
            return true;
        }
        return last.animalType == animal.animalType
    }

    spliceAnimal(animal:level_bunchAnimalItem){
        let index:number = this.checkIndexByAnimal(animal);
        if(index > -1){
            this.animalAry.splice(index,1);
        }
    }

    checkIndexByAnimal(animal:level_bunchAnimalItem){
        return this.animalAry.findIndex((item)=>{return item.uuid == animal.uuid});
    }

    addAnimal(animal:level_bunchAnimalItem){
        this.animalAry.push(animal);
        animal.node.zIndex = this.maxCount - this.animalAry.length;
    }

    checkGroupIsVaild(){
        if(this.animalAry.length < this.maxCount){
            return false;
        }
        let last = this.getLastTypeAnimal();
        let lastType = last.animalType;
        for(let i = this.animalAry.length -2; i>=0; i--){
            let animal = this.animalAry[i];
            if(lastType != animal.animalType){
                return false;
            }
        }
        return true;
    }

    getMoveAnimals(){
        let animals:level_bunchAnimalItem[] = [];
        if(this.animalAry.length > 0){
            let lastAnimal = this.getLastTypeAnimal();
            for(let i = this.animalAry.length -1; i>=0; i--){
                let animal = this.animalAry[i];
                if(animal.animalType != lastAnimal.animalType){
                    break;
                }else{
                    animals.push(animal);
                }
            }
        }
        return animals;
    }
    
    getCanAddAnimalCount(){
        return this.maxCount - this.animalAry.length;
    }

    setHintLb(str:string){
        cc.Tween.stopAllByTarget(this.showNode);
        this.hintLb.string = str;
        this.hintNode.active = true;
        cc.tween(this.showNode).delay(2).call(()=>{
            this.hideHint();
        }).start();
    }

    hideHint(){
        this.hintNode.active = false;
    }

}
